﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RPGX.Data;
using RPGXEngine.Server.ActionMessages;
using RPGXEngine.Server;

namespace RPGX.Server
{
    public static class ActionMessageRepository
    {
        #region Public Methods

        public static List<IServerActionMessage> GetActionMessages()
        {
            var context = new RPGXServerEntities();

            var messages = from m in context.ServerActionMessages.AsEnumerable()
                           where m.Actioned == false
                           select ActionMessageProcessor.FromDBMessage(m);

            return messages.ToList();
        }

        public static void MarkMessageAsActioned(IServerActionMessage message)
        {
            var context = new RPGXServerEntities();

            var m = (from me in context.ServerActionMessages
                     where me.ServerActionMessageId == message.ActionMessageId
                     select me).FirstOrDefault();

            m.Actioned = true;

            context.SaveChanges();
        }

        public static IServerActionMessage GetSystemResumeMessage()
        {
            var context = new RPGXServerEntities();

            var m = (from me in context.ServerActionMessages
                     where me.Message == (int)ServerActionMessages.Resume
                        && me.Actioned == false
                     select me).FirstOrDefault();

            return m == null ? null : ActionMessageProcessor.FromDBMessage(m);
        }

        #endregion
    }
}
